﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 6. Write a program that finds the most frequent 
		number in an array. Example: 
		  {4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3} -> 4 (5 times)  -->
</head>
<body>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		var array = new Array();
		var number = parseInt(prompt("Enter number of elements"));
		if (!isNaN(number)) {
			// Enter the array of numbers
			for (var i = 0, j = 0; i < number; i++) {
				var n = parseInt(prompt("Enter element " + i));
				if (!isNaN(n)) {
					array[j] = n;
					j++;
				}
			}

			jsConsole.writeLine("You have entered: ");
			jsConsole.writeLine(array.join());
			jsConsole.writeLine("");

			// Sort array
			array.sort();
			
			var currentNumber = array[0];
			var count = 1;
			var maxCount = 1;
			var maxNumber = array[0];
			for (var i = 1; i < array.length; i++)
			{
				if (array[i] == currentNumber)
				{ count++; }
				else
				{
					if (count > maxCount)
					{
						maxCount = count;
						maxNumber = currentNumber;
					}

					count = 1;
					currentNumber = array[i];
				}
			}

			jsConsole.writeLine("Most frequent number is:");
			jsConsole.writeLine(maxNumber + " (" + maxCount + " times)");
		}
		else {
			jsConsole.writeLine("Wrong Input.");
		}
	</script>
</body>
</html>
